package com.zs.framework.reference;

import org.springframework.util.ConcurrentReferenceHashMap;

import javax.validation.constraints.NotNull;
import java.util.HashMap;

/**
 * WeakReferenceTest
 *
 * @author zhougf
 * @date 2020/09/03
 */
public class WeakReferenceTest {
    public static void main(String[] args) {
        WeakReferenceTest weakReferenceTest = new WeakReferenceTest();
//        weakReferenceTest.referenceMap();
//        weakReferenceTest.hashMap();
        weakReferenceTest.nonNull(null);
    }

    private void referenceMap() {
        ConcurrentReferenceHashMap map = new ConcurrentReferenceHashMap(16, ConcurrentReferenceHashMap.ReferenceType.WEAK);
        map.put("key","val");
        System.out.println("referenceMap:" + map);
        System.gc();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("referenceMap:" + map);
    }

    private void hashMap() {
        HashMap<String, String> map = new HashMap<>(16);
        map.put("key","val");
        System.out.println("hashMap:" + map);
        System.gc();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("hashMap:" + map);
    }

    private String nonNull(@NotNull String s) {
        System.out.println(s);
        return s;
    }
}
